import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-11-02
 * Time: 15:30
 */

//二叉搜索树
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    public TreeNode search(int val) {
        TreeNode cur = root;

        while(cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            } else if(cur.val > val) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }

    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        TreeNode cur = root;
        TreeNode parent = null;

        if(cur == null) {
            root = node;
            return true;
        }

        while(cur != null) {
            if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }
        if(parent.val > key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }

    //删除一个节点
    public void removeNode (int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                remove(cur, parent);
                return;
            }
        }
    }

    private void remove (TreeNode cur, TreeNode parent) {
        if(cur.left == null) {
            if(root == cur) {
                root = cur.right;
            } else if (parent.right == cur) {
                parent.right = cur.right;
            } else {
                parent.left = cur.right;
            }
        } else if (cur.right == null) {
            if(root == cur) {
                root = cur.left;
            } else if (parent.left == cur) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;

            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }

            cur.val = target.val;

            if(targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }

    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(12);
        binarySearchTree.insert(5);
        binarySearchTree.insert(8);
        binarySearchTree.insert(4);
        binarySearchTree.insert(32);

        //binarySearchTree.Remove(5);
        binarySearchTree.removeNode(5);
        binarySearchTree.removeNode(32);


        System.out.println("sdfas");

    }
}
